package com.united.ucts.modules.system.controller;

import com.united.frame.common.module.result.PageResult;
import com.united.frame.common.module.result.Result;
import com.united.ucts.modules.system.pojo.po.Resource;
import com.united.ucts.modules.system.service.ResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 后台资源管理Controller
 *
 * @author Hudan
 * @version 1.0
 * @filename ResourceController.java
 * @copyright <a href="www.united.cn">www.united.cn</a>
 * @date 2019-08-16
 */
@Api(tags = "ResourceController", value = "后台资源管理")
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/resource")
public class ResourceController {

    private final ResourceService resourceService;

    @ApiOperation("添加后台资源")
    @PostMapping(value = "/create")
    public Result<Boolean> create(@RequestBody Resource resource) {
        boolean result = resourceService.create(resource);
        if (result) {
            return Result.success(true);
        } else {
            return Result.failure();
        }
    }

    @ApiOperation("根据ID删除后台资源")
    @PostMapping(value = "/delete/{id}")
    public Result<Boolean> delete(@PathVariable String id) {
        boolean result = resourceService.delete(id);
        if (result) {
            return Result.success(true);
        } else {
            return Result.failure();
        }
    }

    @ApiOperation("修改后台资源")
    @PostMapping(value = "/update/{id}")
    public Result<Boolean> update(@PathVariable String id,
                                  @RequestBody Resource resource) {
        boolean result = resourceService.update(id, resource);
        if (result) {
            return Result.success(true);
        } else {
            return Result.failure();
        }
    }

    @ApiOperation("根据ID获取资源详情")
    @GetMapping(value = "/{id}")
    public Result<Resource> getItem(@PathVariable String id) {
        return Result.success(resourceService.detail(id));
    }

    @ApiOperation("分页模糊查询后台资源")
    @GetMapping(value = "/list")
    public Result<PageResult<Resource>> pageBy(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                               @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                               @RequestParam(required = false) String categoryId,
                                               @RequestParam(required = false) String nameKeyword,
                                               @RequestParam(required = false) String urlKeyword) {
        List<Resource> resources = resourceService.pageBy(pageNum, pageSize, categoryId, nameKeyword, urlKeyword);
        return Result.success(PageResult.restPage(resources));
    }

    @ApiOperation("查询所有后台资源")
    @GetMapping(value = "/listAll")
    public Result<List<Resource>> listAll() {
        return Result.success(resourceService.listAll());
    }

    @ApiOperation("初始化资源角色关联数据")
    @GetMapping(value = "/initResourceRolesMap")
    public Result<Map<String, List<String>>> initResourceRolesMap() {
        return Result.success(resourceService.initResourceRolesMap());
    }
}